package com.cez.itstudio01.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cez.itstudio01.mapper.UserMapper;
import com.cez.itstudio01.pojo.Book;
import com.cez.itstudio01.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * author: chenez
 * date: 2022/9/12 19:36
 * description:
 */
@Controller
public class userController {
    @Autowired
    UserMapper userMapper;
    @GetMapping("/index/admin/user")
    public String user(){
        return "redirect:/admin/user.html";
    }
    /**
     * @Description 查询
     * @Date 2022/9/12 23:15
     * @param page
     * @param limit
     * @param map1
     *@return java.lang.Object
     **/
    @GetMapping("/admin/getUserJson")
    @ResponseBody
    public  Object getUser(@RequestParam("page") Integer page,
                           @RequestParam("limit") Integer limit,
                           @RequestParam(required = false) Map map1){
        String ID=null;
        String name=null;
        String limit1=null;
        String searchParams = (String) map1.get("searchParams");
        Map maps = (Map) JSON.parse(searchParams);
        if(maps!=null) {
            ID = (String) maps.get("ID");
            name = (String) maps.get("name");
            limit1 = (String) maps.get("limit");
        }
        //List<User> users = userMapper.get(page-1, limit, Wrappers.<User>lambdaQuery().eq(StringUtils.isNotBlank(ID),User::getId,ID).eq(StringUtils.isNotBlank(name),User::getName,name).eq(StringUtils.isNotBlank(limit1),User::getLimit,limit1));
        int page1=(page-1)*limit;
        List<User> users = userMapper.get(page1, limit, ID, name, limit1);
        for (User u : users) {
            if(u.getLimit().equals("1")){
                u.setLimit("管理员");
            }else{
                u.setLimit("学生");
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        Long count = userMapper.selectCount(null);
        map.put("code",0);
        map.put("message","");
        map.put("count",count);
        map.put("data",users);
        return  map;
    }
    @GetMapping("/admin/add_user")
    public String addUser(){return "/admin/add_user.html";}
   /**
    * @Description 添加用户
    * @Date 2022/9/14 21:24
    * @param ID
    * @param name
    * @param limit
    * @param password
    *@return java.lang.Object
    **/
    @PostMapping("/index/admin/addUserJson")
    @ResponseBody
    public Object addUserJson(String ID,String name,String limit,String password){
        User user=null;
        if(password!=null){
            user=new User(ID,name,password,limit,null,null,null);
        }else {
            user = new User(ID, name, "123456", limit, null, null, null);
        }
        userMapper.addUser(user);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("message","添加成功");
        return map;
    }
    @PostMapping("/index/admin/deleteUserJson")
    @ResponseBody
    public Object deleteUser(Integer id){
        userMapper.deleteById(id);
        HashMap<Object, Object> res = new HashMap<>();
        res.put("code",1);
        res.put("message","删除成功");
        return res;
    }
    @GetMapping("/admin/edit_user")
    public String editUser(){return "/admin/edit_user.html";}
    @PostMapping("/index/admin/deleteAllUserJson")
    @ResponseBody
    public Object deleteBatch(@RequestParam Map post_data){
        Set key = post_data.keySet();
        ArrayList list = new ArrayList<>();
        for(Object key1:key){
            list.add(post_data.get(key1));
        }
        ArrayList arrayList=new ArrayList<>();
        for(int i=0;i<= list.size()-1;i=i+7){
            Object ID = list.get(i);
            arrayList.add(ID);
        }
        userMapper.deleteBatchIds(arrayList);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("message","删除成功");
        return map;
    }
}
